package com.trace.app.entity;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.trace.app.entity.consts.AppHttpCodeEnum;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

@Data
@Accessors(chain = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseResult<T> implements Serializable {
    private Integer code;
    private String message;
    private T result;

    public ResponseResult() {
        this.code = AppHttpCodeEnum.SUCCESS.getCode();
        this.message = AppHttpCodeEnum.SUCCESS.getMessage();
    }

    public ResponseResult(Integer code, T result) {
        this.code = code;
        this.result = result;
    }

    public ResponseResult(Integer code, String message, T result) {
        this.code = code;
        this.message = message;
        this.result = result;
    }

    public ResponseResult(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public static ResponseResult errorResult(int code, String message) {
        ResponseResult result = new ResponseResult();
        return result.error(code, message);
    }

    public static ResponseResult okResult() {
        ResponseResult result = new ResponseResult();
        return result;
    }

    public static ResponseResult okResult(int code, String message) {
        ResponseResult result = new ResponseResult();
        return result.ok(code, null, message);
    }

    public static ResponseResult okResult(Object result) {
        ResponseResult res = setAppHttpCodeEnum(AppHttpCodeEnum.SUCCESS, AppHttpCodeEnum.SUCCESS.getMessage());
        if (result != null) {
            res.setResult(result);
        }
        return res;
    }

    public static ResponseResult errorResult(AppHttpCodeEnum enums) {
        return setAppHttpCodeEnum(enums, enums.getMessage());
    }

    public static ResponseResult errorResult(AppHttpCodeEnum enums, String message) {
        return setAppHttpCodeEnum(enums, message);
    }

    public static ResponseResult setAppHttpCodeEnum(AppHttpCodeEnum enums) {
        return okResult(enums.getCode(), enums.getMessage());
    }

    private static ResponseResult setAppHttpCodeEnum(AppHttpCodeEnum enums, String message) {
        return okResult(enums.getCode(), message);
    }

    public ResponseResult<?> error(Integer code, String message) {
        this.code = code;
        this.message = message;
        return this;
    }

    public ResponseResult<?> ok(Integer code, T result) {
        this.code = code;
        this.result = result;
        return this;
    }

    public ResponseResult<?> ok(Integer code, T result, String message) {
        this.code = code;
        this.result = result;
        this.message = message;
        return this;
    }

    public ResponseResult<?> ok(T result) {
        this.result = result;
        return this;
    }


}